<template>
  <div>
    <el-dialog :title="info.isAdd?'角色添加':'角色编辑'" :visible.sync="info.isshow"  @closed="close">
      <el-form :model="form">
        {{ form }}
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="menuList"
            show-checkbox
            node-key="id"
            ref="tree"
            :default-expanded-keys="[1, 2]"
            :props="{ children: 'children', label: 'title' }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd">添 加</el-button>
        <el-button type="primary" v-else @click="xiugai">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqroleadd, reqmenulist,reqroleinfo,reqroleedit } from "../../../request/api";
import { IndexRouters } from "../../../router";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info", "list"],
  data() {
    return {
      menuList: [],
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
      menulist: [],
      data: [
        {
          id: 1,
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1",
            },
          ],
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1",
            },
            {
              id: 6,
              label: "二级 2-2",
            },
          ],
        },
      ],
      IndexRouters,
    };
  },
  computed: {
    ...mapGetters({
      user:"user"
    }),
  },
  mounted() {
      //请求菜单数据
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.menuList = res.data.list ? res.data.list : [];
        console.log(this.menuList);
      }
    });
  },
  methods: {
    ...mapActions({
      changeuser:"changeuser"
    }),
        //取消
    quxiao() {
      this.info.isshow = false;
    },
    //清空form
    qingkong() {
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
    },
    //获取一条数据
    getOne(id) {
      //取数据
      reqroleinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;//{rolename: "管理员", menus: "[21,23,24,22,25,26]", status: 1}
          // //form此时没有id
          this.form.id = id;
          //将form上的menus赋值给tree展示
          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus))
        }
      });
    },
    //添加
    add() {
      // form.menus 没有数据，数据在tree
      // 1.将tree上的数据赋值给form.menus
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());

      reqroleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //弹出成功
          successAlert(res.data.msg);
          //清空数据
          this.qingkong();
          //关闭弹窗
          this.info.isshow = false;
          //列表重新渲染
          this.$emit("init");
        }
      });
    },
     //修改
    xiugai() {
      //修改请求
      reqroleedit(this.form).then((res) => {
        //修改成功
        if (res.data.code == 200) {
          // 1.弹成功
          successAlert(res.data.msg);
          // 3.弹框消失
          this.info.isshow = false;
        
          if(this.form.id==this.user.roleid){
              this.changeuser({})
              this.$router.push("/login")
              return
          }
           // 2.表单清空
          this.qingkong();
            // 4.列表更新
          this.$emit("init");
        }
      });
    },
    //弹框关闭
    close(){
      // 是编辑，要清空表单
      !this.info.isAdd&&this.qingkong()
    }
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>